//  ========== 
//  = 我的点点专享产品列表 业务JS = 
//  ==========
var limit = 8;
var page1 = 1;
var page2 = 1;
var page3 = 1;
var page4 = 1;
var Main = function(){
	mui.init({
		swipeBack:true 		//启用右滑关闭功能
	});
	this.init();
	this.proj(0,1);
};
Main.prototype = {

	init : function(){
		var deceleration = mui.os.ios?0.003:0.0009;
		mui('.mui-scroll-wrapper').scroll({
			bounce: false,
			indicators: true, //是否显示滚动条
			deceleration:deceleration
		});
		var _this = this;
		mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
		mui(pullRefreshEl).pullToRefresh({
			down: {
				callback: function() {
				   var self = this;
				   	   if(index == 0 ){
                           page1=1;					   	
					   }
					   if(index == 1 ){
                           page2=1;	
					   }
					   if(index == 2 ){
                           page3=1;					   	
					   }
					   if(index == 3 ){
                           page4=1;					   	
					   }
					_this.proj(index,1,function(){
						_this._prodBindTap()
						self.endPullDownToRefresh();
					});
				}
			},
			up: {
				callback: function() {
					var self = this;
					var page ;
						if(index == 0 ){
                           ++page1;	
                           page = page1; 
					   }
					   if(index == 1 ){
                           ++page2;		
                           page = page2;
					   }
					   if(index == 2 ){
                           ++page3;
                           page = page3;
					   }
					   if(index == 3 ){
                           ++page4;
                           page = page4;
					   }
					_this.proj(index,page,function(){
						_this._prodBindTap();
						self.endPullUpToRefresh();
					});

				}
			}
		});
	  });
					
		var _this = this;
		
		_this.eventInit()
		
	},
	eventInit : function(){//绑定事件
		var _this = this;
        $(".mui-control-item").each(function(index){
        	this.addEventListener('tap',function(){
        		page = 1;
        		_this.proj(index,page);
        	});
        });
	},
	_prodBindTap : function(index,param){//列表绑定事件
		$('.m-btn-payment').on('tap',function(){//回款
			event.stopPropagation();
			var orderId = this.getAttribute('data-id');
			var url = 'myBidSalePaymentPlan.html?orderId='+ orderId;
			mui.openWindow(url);
		});
		$('.m-btn-bond').on('tap',function(){//债券
			event.stopPropagation();
		   var orderId = this.getAttribute('data-id');
		});
		$('.m-btn-contract').on('tap',function(){//合同
			event.stopPropagation();
			var orderId = this.getAttribute('data-id');
			mui.openWindow('buyInfoUContract.html?orderId='+orderId);
		});
		$('.m-btn-quit').on('tap',function(){//退出
			event.stopPropagation();
			var orderId = this.getAttribute('data-id');
			var lockingenddate = this.getAttribute('data-lockingenddate');
			if(lockingenddate.replace(/[- :]/g,'')>Util.time.formatDate(new Date(),'yyyyMMddhhmmss')){
				Util.toast("还在锁定期，不能提前支取！");
			}else{
				
			}
		});
			mui(".m-sale-list").on('tap','li',function(e){
				 var target = $(e.target);
				 if(target.attr('class') =='m-btn-payment' || target.attr('class') == 'm-btn-contract'){//合同和回款按钮禁止触发
				 	return false;
				 }
				mui.openWindow({
					    url:'bidSaleDetail.html?buyFlag=false&mmplanId='+this.getAttribute('data-id'),
					    id:'bidSaleDetail',
					    extras:param
				     }
				);
			});
	},

	//项目列表
	proj : function(index,page,_callback){
		var _this = this;
		var param = {
				isMobile:1,
				memberId:Util.getLoginUser().memberId,
				page:page,
				limit:8
				};
		var url = server_url+'/creditFlow/financingAgency/managelistPlManageMoneyPlanBuyinfo.do';
		var currTab;
        if(index == 0){//点点专享 - 购买中
       	     currTab = $('#item1ul');
       	     param.state1 = 1;
       	     param.show = 'buyingList';
		}else if(index == 1){//点点专享 - 持有中
			 currTab = $('#item2ul');
			 param.state2 = 2;
			 param.state7 = 7;
			 param.show = 'buyingList';
		}else if(index == 2){//点点安盈 完成
			 currTab = $('#item3ul');
			 param.state10 = 10;
			 param.show = 'successList';
		}else if(index == 3){//点点安盈 已退出
			 currTab = $('#item4ul');
			 param.state8 = 8;
			 param.show = 'outList';
		}
		$.getJSON(url,param  ,function(data){
			var _html = '';
            if(data.success){
            	if(index ==0 ){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.plManageMoneyPlan.mmplanId+'">';
	            		 /* _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';*/
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit">'+item.mmName+'</h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.yeaRate+'</span>';
	            		  if(item.plManageMoneyPlan.addRate && item.plManageMoneyPlan.addRate>0){
	            		  _html+='\n <span class="m-small">+'+item.plManageMoneyPlan.addRate+'</span>';
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.investlimit+'</span>';
	            		  _html+='\n <span class="m-unit">天</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">项目期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.buyMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-range-bar z-act">';
	            		  _html+='\n <i style="width: '+item.plManageMoneyPlan.progress+'%;"></i>';
	            		  _html+='\n <div class="m-range-round" style="left: '+item.plManageMoneyPlan.progress+'%;">';
	            		  _html+='\n <div class="round"></div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table m-proj-nottom">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5">';
	            		  _html+='\n <h4 class="mui-ellipsis m-proj-tit">';
	            		  _html+='\n 投标日期:<b class="txt">'+item.buyDatetime+'</b>';
	            		  _html+='\n </h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
	            		  _html+='\n <span class="mui-h5 m-proj-tag">'+item.plManageMoneyPlan.progress+'%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </li>';
					}	
            	}else if(index == 1 ){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.plManageMoneyPlan.mmplanId+'">';
	            		 /* _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';*/
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit-big">'+item.mmName+'</h4>';
						  _html+='\n 	<div class="m-proj-tit-small">下期派息:2/3期 累计回款:1000元</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div style="text-align: right;">收益中</div>';
	            		  _html+='\n </div>';
						  _html+='\n <div class="m-range-bar z-act"></div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.yeaRate+'</span>';
	            		  if(item.plManageMoneyPlan.addRate && item.plManageMoneyPlan.addRate>0){
	            		  _html+='\n <span class="m-small">+'+item.plManageMoneyPlan.addRate+'</span>';
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.investlimit+'</span>';
	            		  _html+='\n <span class="m-unit">天</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">项目期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.buyMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  
	            		  //按钮
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-proj-rt-btn">';
	            		  _html+='\n <input type="button" class="m-btn-payment" value="回款" data-id="'+item.orderId+'"/>';
	            		  //_html+='\n <input type="button" class="m-btn-bond" value="债券" data-id="'+item.orderId+'"/>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-rt-btn">';
	            		  _html+='\n <input type="button" class="m-btn-contract" value="合同" data-id="'+item.orderId+'"/>';
	            		  if(item.state == '7'){
	            		  //_html+='\n <input type="button" class="" value="退审中" data-id="'+item.orderId+'"/>';	
	            		  }else{
	            		  //_html+='\n <input type="button" class="m-btn-quit" value="退出" data-id="'+item.orderId+'" data-lockingEndDate="'+item.plManageMoneyPlan.lockingEndDate+'"/>';
	            		  }
	            		  _html+='\n </div>';
	            		  
	            		  _html+='\n </div>';
	            		  _html+='\n </li>';
					}	
            	}else if(index == 2){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <!--list beg-->';
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.plManageMoneyPlan.mmplanId+'">';
	            		 /* _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';*/
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit">'+item.mmName+'</h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.yeaRate+'</span>';
	            		  if(item.plManageMoneyPlan.addRate && item.plManageMoneyPlan.addRate>0){
	            		  _html+='\n <span class="m-small">+'+item.plManageMoneyPlan.addRate+'</span>';
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.investlimit+'</span>';
	            		  _html+='\n <span class="m-unit">天</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">项目期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.buyMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-range-bar z-act">';
	            		  _html+='\n <i style="width: 100%;"></i>';
	            		  _html+='\n <div class="m-range-round" style="left: 100%;">';
	            		  _html+='\n <div class="round"></div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table m-proj-nottom">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5">';
	            		  _html+='\n <h4 class="mui-ellipsis m-proj-tit">';
	            		  _html+='\n 投标日期:<b class="txt">'+item.buyDatetime+'</b>';
	            		  _html+='\n </h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
	            		  _html+='\n <span class="mui-h5 m-proj-tag">100%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </li>';
	            		  _html+='\n <!-- list end-->';
					}	
            	}else{
            		
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		  _html+='\n <!--list beg-->';
	            		  _html+='\n <li class="mui-table-view-cell" data-id="'+item.plManageMoneyPlan.mmplanId+'">';
	            		 /* _html+='\n <!--右上角状态 beg-->';
	            		  _html+='\n 	<div class="m-proj-rt-tips">';
	            		  _html+='\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
	            		  _html+='\n </div>';*/
	            		  _html+='\n <!--右上角状态 end-->';
	            		  _html+='\n 	<div class="mui-table">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-10">';
	            		  _html+='\n 	<h4 class="mui-ellipsis m-proj-tit">'+item.mmName+'</h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-info">';
	            		  _html+='\n <div class="m-proj-box m-proj-lt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.yeaRate+'</span>';
	            		  if(item.plManageMoneyPlan.addRate && item.plManageMoneyPlan.addRate>0){
	            		  _html+='\n <span class="m-small">+'+item.plManageMoneyPlan.addRate+'</span>';
	            		  }
	            		  _html+='\n <span class="m-unit">%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">年化利率</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-cn">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.plManageMoneyPlan.investlimit+'</span>';
	            		  _html+='\n <span class="m-unit">天</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">项目期限</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-proj-box m-proj-rt">';
	            		  _html+='\n <div class="m-info-top">';
	            		  _html+='\n <span class="m-big">'+item.buyMoney+'</span>';
	            		  _html+='\n <span class="m-unit">元</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-info-des">加入金额</div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="m-range-bar z-act">';
	            		  _html+='\n <i style="width: 100%;"></i>';
	            		  _html+='\n <div class="m-range-round" style="left: 100%;">';
	            		  _html+='\n <div class="round"></div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table m-proj-nottom">';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5">';
	            		  _html+='\n <h4 class="mui-ellipsis m-proj-tit">';
	            		  _html+='\n 投标日期:<b class="txt">'+item.buyDatetime+'</b>';
	            		  _html+='\n </h4>';
	            		  _html+='\n </div>';
	            		  _html+='\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
	            		  _html+='\n <span class="mui-h5 m-proj-tag">50%</span>';
	            		  _html+='\n </div>';
	            		  _html+='\n </div>';
	            		  _html+='\n </li>';
	            		  _html+='\n <!-- list end-->';
					}	
            	
            	}
            	
            }			
            if(page == 1){
            	currTab.html('');
            }

			currTab.append(_html);

            if(_this._prodBindTap){
			  _this._prodBindTap();
            }             
            if($.isFunction(_callback)){
              _callback();
 			}
		});
	}
	
}
